import javax.swing.*;
import java.awt.*;

public class CapacitorCaculator extends JFrame {

    private JPanel contentPane;
    private JTextField tfIn;
    private JTextField tfOut;

    public CapacitorCaculator() {
        setTitle("电容计算器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 460, 120);

        contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        setContentPane(contentPane);
        JLabel lbl1 = new JLabel("电容标号");
        tfIn = new JTextField();
        tfIn.setColumns(20);


        JLabel lbl2 = new JLabel("电容值");
        tfOut = new JTextField();
        tfOut.setColumns(20);

        JRadioButton rb1 = new JRadioButton("标号转容值");
        JRadioButton rb2 = new JRadioButton("容值转标号");
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(rb1);
        buttonGroup.add(rb2);

        JButton btn = new JButton("计算");
        contentPane.add(lbl1);
        contentPane.add(tfIn);

        contentPane.add(lbl2);
        contentPane.add(tfOut);

        contentPane.add(rb1);
        rb1.setSelected(true);
        contentPane.add(rb2);
        contentPane.add(btn);
        btn.addActionListener(e -> {
            String text1 = tfIn.getText();
            String text2 = tfOut.getText();
            if (rb1.isSelected()) {
                String s = numToVal(text1);
                if (s != null) {
                    tfOut.setText(s);
                } else {
                    JOptionPane.showMessageDialog(CapacitorCaculator.this, "输入格式错误！");
                }
            } else if (rb2.isSelected()) {
                String s = valToNum(text2);
                if (s != null) {
                    tfIn.setText(s);
                } else {
                    JOptionPane.showMessageDialog(CapacitorCaculator.this, "输入格式错误！");
                }
            }
        });

    }

    //电容标号转换为容值
    private String numToVal(String numStr) {

        int num;
        try {
            num = Integer.parseInt(numStr);
        } catch (NumberFormatException nfe) {
            return null;
        }

        if (numStr.length() <= 2) {
            return num + "pF";
        } else if (numStr.length() != 3) {
            return null;
        }
        //numStr.length()==3
        String res = null;
        long val = (num / 10) * (long) Math.pow(10, num % 10);//103 = 10*10^3
        if (val < 1e3) {
            res = val + "pF";
        } else if (val < 1e6) {
            double ans = val / 1e3;
            if (ans % 1 == 0) {//如果小数部分是0
                res = (long) ans + "nF";
            } else {
                res = ans + "nF";
            }
        } else {
            double ans = val / 1e6;
            if (ans % 1 == 0) {//如果小数部分是0
                res = (long) ans + "uF";
            } else {
                res = ans + "uF";
            }
        }
        return res;

    }

    //容值转化为电容标号
    private String valToNum(String valStr) {
        double mul;//设置不同单位的倍率
        if (valStr.endsWith("pF")) {
            mul = 1;
        } else if (valStr.endsWith("nF")) {
            mul = 1e3;
        } else if (valStr.endsWith("uF")) {
            mul = 1e6;
        } else {
            return null;
        }

        double val;
        try {
            val = Double.parseDouble(valStr.substring(0, valStr.length() - 2));
        } catch (NumberFormatException nfe) {
            return null;
        }

        if (val < 100 && mul == 1) {//如果小于100pF
            return (int) val + "";
        }

        val = val * mul;//pF
        //System.out.println("val:" + val);
        int p = (int) (Math.log10(val)) - 1;
        //System.out.println("p:" + p);
        val = val / Math.pow(10, p);
        //System.out.println("res:" + (int) val);
        return (int) val + "" + p;

    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel.class.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        CapacitorCaculator capacitorCaculator = new CapacitorCaculator();
        capacitorCaculator.setVisible(true);
    }
}
